$(function () {
	/*商品列表，鼠标移入时加阴影、移出移除阴影*/
	$(".goods-panel").hover(function () {
		$(this).css("box-shadow", "0px 0px 8px #888888");
	}, function () {
		$(this).css("box-shadow", "");
	})
	//加入收藏时，♥的实心空心切换
	$(".add-fav").toggle(function () {
		$(this).html("<span class='fa fa-heart'></span>取消收藏");
	}, function () {
		$(this).html("<span class='fa fa-heart-o'></span>加入收藏");
	})
})

import {
	checkLogin
} from './auth.js'
new Vue({
	el: '#app',
	data: {
		IS_LOGIN: null,
		searchKey: '', // 搜索的关键词
		pathSearchKey: '',
		productList: [],
		currentPage: 1,
		pageTotal: 0,
		// 商品列表1
		productListRow1: [],
		// 商品列表2
		productListRow2: [],
		// 商品列表3
		productListRow3: []
	},
	async created() {
		this.IS_LOGIN = await checkLogin()
	},
	mounted() {
		const path = window.location.href
		// 获取搜索关键词,如果没有搜索关键词，则置空串
		if (path.indexOf('=') === -1) {
			this.pathSearchKey = ''
		} else {
			// 获取搜索关键词
			this.pathSearchKey = decodeURIComponent(path.substring(path.lastIndexOf('=') + 1))
		}
		console.log('search.js::this.pathSearchKey:' + this.pathSearchKey);
		// 获取商品列表
		this.getPageList();
	},
	methods: {
		// 加入收藏
		joinLove(itemId) {
			console.log('加入收藏:', itemId);
			this.$message.success('加入收藏成功')
		},
		// 加入购物车
		async joinCart(itemId) {
			console.log('加入购物车:', itemId);
			const res = await request.post('/cart/addItem',{
				itemId: itemId,
				num: 1
			})
			console.log('search.js:res:',res);
			this.$message.success('加入购物车成功')
		},
		// 切换上一页
		prePage() {
			if (this.currentPage > 1) {
				this.currentPage--
				this.getPageList()
			}
		},
		// 切换下一页
		nextPage() {
			if (this.currentPage < this.pageTotal) {
				this.currentPage++
				this.getPageList()
			}
		},
		// 获取商品列表
		async getPageList() {
			const res = await request.post('/product/searchProductsByPage', {
				currentPage: this.currentPage,
				pageSize: 12,
				searchKey: this.pathSearchKey
			})
			this.productList = res.data.pageList
			this.pageTotal = res.data.pageTotal
			this.currentPage = res.data.currentPage
			// console.log('search.js:productList:', this.productList);
			// console.log('search.js:pageTotal:', this.pageTotal);
			// console.log('search.js:currentPage:', this.currentPage);
			// console.log('search.js:res:', res);
			this.productListRow1 = []
			this.productListRow2 = []
			this.productListRow3 = []
			for (let index = 0; index < this.productList.length; index++) {
				const element = this.productList[index];
				if (index <= 3) {
					this.productListRow1.push(element)
				} else if (index <= 7) {
					this.productListRow2.push(element)
				} else {
					this.productListRow3.push(element)
				}
			}
		},
		// 跳转到商品详情页
		gotoProduct(itemId) {
			// console.log(itemId);
			location.href = `/web/product.html?productId=${itemId}`
		},
		// 查找商品
		searchProduct() {
			// console.log('查找商品:');
			location.href = `../web/search.html?searchKey=${this.searchKey}`
		},
		// 退出登录
		logout() {
			localStorage.removeItem('token')
			this.IS_LOGIN = null
			this.$message.success('退出成功')
		},
	}
})